#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_set>
using namespace std;


struct ListNode{
    int val;
    ListNode* next;
    ListNode(int x): val(x), next(nullptr){}
};

class Solution{
    public:

    ListNode* ans1(ListNode* head, int x){
        ListNode* lowhead = new ListNode(0);
        ListNode* heighhead = new ListNode(x);
        ListNode* lowtail = lowhead;
        ListNode* heightail = heighhead;
        ListNode* temp = head;
        if(!head){
            return head;
        }
        while(temp){
            if(temp->val < x){
                lowtail->next = new ListNode(temp->val);
                lowtail = lowtail->next;
            }else{
                heightail->next = new ListNode(temp->val);
                heightail = heightail->next;
            }
            temp = temp->next;
        }
        ListNode* ret;
        if(lowhead->next == nullptr){
            ret = heighhead->next;
        }else{
            ret = lowhead->next;
            lowtail->next = heighhead->next;
        }
        
        return ret;
    }

};

void test1(){
    vector<int> num = {1,4,3,2,5,2};
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(int i = 0; i != num.size(); i++){
        if(!head){
            head = tail = new ListNode(num[i]);
        }else{
            tail->next = new ListNode(num[i]);
            tail = tail->next;
        }
    }
    int x = 3;

    Solution s;
    ListNode* ret = s.ans1(head, x);
    while(ret){
        cout << ret->val << " " << ends;
        ret = ret->next;
    }
    cout << endl;
}

void test2(){
    vector<int> num = {2,1};
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(int i = 0; i != num.size(); i++){
        if(!head){
            head = tail = new ListNode(num[i]);
        }else{
            tail->next = new ListNode(num[i]);
            tail = tail->next;
        }
    }
    int x = 4;

    Solution s;
    ListNode* ret = s.ans1(head, x);
    while(ret){
        cout << ret->val << " " << ends;
        ret = ret->next;
    }
    cout << endl;
}

void test3(){
    vector<int> num = {1};
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(int i = 0; i != num.size(); i++){
        if(!head){
            head = tail = new ListNode(num[i]);
        }else{
            tail->next = new ListNode(num[i]);
            tail = tail->next;
        }
    }
    int x = 0;

    Solution s;
    ListNode* ret = s.ans1(head, x);
    while(ret){
        cout << ret->val << " " << ends;
        ret = ret->next;
    }
    cout << endl;
}

int main(void){
    test1();
    test2();
    test3();

    return 0;
}